import guicomponents.*;


GPanel pnlControls;
GCombo cboColor, cboLabel, cboSize, cboteste;
GOption optHand, optXhair, optMove, optText,optWait;

PImage mapImage;
Table locationTable;
int rowCount;

Table dataTable;
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;
float dataMin2 = MAX_FLOAT;
float dataMax2 = MIN_FLOAT;
float dataMin3 = MAX_FLOAT;
float dataMax3 = MIN_FLOAT;
  float percent = 0;


void setup() {
  size(1300, 574);
  

  
  createCombos();
  mapImage = loadImage("mapa3.png");
  locationTable = new Table("locations.tsv");
  rowCount = locationTable.getRowCount();
  
  // Read the data table
  dataTable = new Table("data.txt");
   // acho os valores maximos e minimos e uso eles em todos os casos
  
    for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 1);
    if (value > dataMax) {
      dataMax = value;
    }
    if (value < dataMin) {
      dataMin = value;
    }
  }
    for (int row = 0; row < rowCount; row++) {
   float value = dataTable.getFloat(row, 2);
    if (value > dataMax2) {
      dataMax2 = value;
    }
    if (value < dataMin2) {
      dataMin2 = value;
    }
  }
    for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 3);
    if (value > dataMax3) {
      dataMax3 = value;
    }
    if (value <
    dataMin3) {
      dataMin3 = value;
    }
  }
 smooth();
  noStroke(); 
 
}


void draw() {
  background(255);

  image(mapImage, 0, 0);
  handleComboEvents2(cboSize, cboColor);
  handleComboEvents(cboLabel);
  
 
}



//funcao que desenhae pinta as elipses e coloca a label de acordo
//com as opcoes marcadas nos menus
//As opcoes possiveis sao: numero de corridas, numero de vitórias
//e numero de podiuns que cada pais ja teve na historia da formula 1

void drawData(float x, float y, String abbrev, int dimension, GCombo combo) {
smooth();
fill(0);
text("Por cor", width - 500, height - 80); 
text("Por label", width - 660, height - 80);
text("Por tamanho", width - 820, height - 80);

  int value = dataTable.getInt(abbrev, 1); 
  int value2 = dataTable.getInt(abbrev, 2); 
  int value3 = dataTable.getInt(abbrev, 3);
  float radius = 0;


if ( dimension == 1 && combo == cboSize) {
    percent = norm(value, dataMin, dataMax);
  color between = lerpColor(#FFFFFF, #4422CC, percent);  
  fill(between);
 
 
 
  radius = map(value, 0, dataMax, 2, 14);
  // Draw an ellipse for this item
 ellipseMode(RADIUS);
  
  ellipse(x, y, radius, radius);
}
if ( dimension == 2 && combo == cboSize) {
   percent = norm(value2, dataMin2, dataMax2);
    color between = lerpColor(#FFFFFF, #4422CC, percent);  
  fill(between);
 
  
  radius = map(value, 0, dataMax, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 3 && combo == cboSize) {
   percent = norm(value3, dataMin3, dataMax3);
    color between = lerpColor(#FFFFFF, #4422CC, percent); 
  fill(between);
 
  
  radius = map(value, 0, dataMax, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 4 && combo == cboSize) {
   percent = norm(value, dataMin, dataMax);
    color between = lerpColor(#FFFFFF, #4422CC, percent);  
    fill(between);
 
 
  radius = map(value2, 0, dataMax2, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 5 && combo == cboSize) {
   percent = norm(value2, dataMin2, dataMax2);
    color between = lerpColor(#FFFFFF, #4422CC, percent);  
    fill(between);
 
 
  radius = map(value2, 0, dataMax2, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 6 && combo == cboSize) {
   percent = norm(value3, dataMin3, dataMax3);
    color between = lerpColor(#FFFFFF, #4422CC, percent); 
    fill(between);
 
 
  radius = map(value2, 0, dataMax2, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}

if ( dimension == 7 && combo == cboSize) {
   percent = norm(value, dataMin, dataMax);
    color between = lerpColor(#FFFFFF, #4422CC, percent);
    fill(between);
 
 
  radius = map(value3, 0, dataMax3, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 8 && combo == cboSize) {
   percent = norm(value2, dataMin2, dataMax2);
    color between = lerpColor(#FFFFFF, #4422CC, percent);
    fill(between);
 
 
  radius = map(value3, 0, dataMax3, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
if ( dimension == 9 && combo == cboSize) {
   percent = norm(value3, dataMin3, dataMax3);
    color between = lerpColor(#FFFFFF, #4422CC, percent);  
    fill(between);
 
 
  radius = map(value3, 0, dataMax3, 2, 14);
  // Draw an ellipse for this itemi
 ellipseMode(RADIUS);

  ellipse(x, y, radius, radius);
}
  //setando para labels
  if (dist(x, y, mouseX, mouseY) < radius+5 && dimension == 1 && 
 combo == cboLabel) {
    fill(0);
    
    // mostra a abrev do país e o número de vitórias 
   text(abbrev+ "," +value, x, y-radius-4);
  }
    if (dist(x, y, mouseX, mouseY) < radius+5 && dimension == 2
    && combo == cboLabel) {
 fill(0);
    
    // mostra a abrev do país e o número de podiuns
   text(abbrev+ ","+ value2, x, y-radius-4);
  }
      if (dist(x, y, mouseX, mouseY) < radius+5 && dimension == 3
    && combo == cboLabel) {
 fill(0);
   
    // mostra a abrev do país e o número de corridas disputadas por todos os pilotos daquele país
   text(abbrev+ ","+ value3, x, y-radius-4);
  }
}
//funcao que cria os menus
public void createCombos() {
  String[] colors = new String[] {
  "n° vitorias", "n° podiums" , "n° corridas" };
  cboColor = new GCombo(this, colors, 3, width - 500, height - 70, 160);
  cboColor.setSelected(0);
  String[] labels = new String[] { 
    "n° vitorias", "n° podiums" , "n° corridas"};
  cboLabel = new GCombo(this, labels, 3,width -  660, height - 70, 160);
  cboLabel.setSelected(0);
    String[] sizes = new String[] { 
    "n° vitorias", "n° podiums", "n° corridas"};
  cboSize = new GCombo(this, sizes, 3, width - 820, height - 70, 160);
    cboSize.setSelected(0);

}

//funcao para comandar as acoes relacionadas a label

public void handleComboEvents(GCombo combo){
  if(cboLabel == combo && cboLabel.selectedIndex() == 0 ){
   for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 1, cboLabel);
  }
   
  }
 
 if(cboLabel == combo && cboLabel.selectedIndex() == 1 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 2, cboLabel);
  }
   
  }
   if(cboLabel == combo && cboLabel.selectedIndex() == 2 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 3, cboLabel);
  }
   
  }
}

//funcao para comandar as acoes relacionadas a cor e tamanho
 public void handleComboEvents2(GCombo combo, GCombo combo2){

 if(cboSize == combo && cboSize.selectedIndex() == 0 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 0 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 1, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 0 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 1 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 2, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 0 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 2 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 3, cboSize);
  }
 }
 }	
  if(cboSize == combo && cboSize.selectedIndex() == 1 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 0 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 4, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 1 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 1 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 5, cboSize);
  }
 }
 }
 
  if(cboSize == combo && cboSize.selectedIndex() == 1 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 2 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 6, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 2 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 0 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 7, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 2 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 1 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 8, cboSize);
  }
 }
 }
  if(cboSize == combo && cboSize.selectedIndex() == 2 ){
   if(cboColor == combo2 && cboColor.selectedIndex() == 2 ){
    for (int row = 0; row < rowCount; row++) {
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev, 9, cboSize);
  }
 }
 }
   
  
   
}




